<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/4/3
 * Time: 23:17
 */

namespace app\admin\controller;


use app\common\lib\Tree;
use app\common\logic\ArticleLogic;
use app\common\logic\CategoryLogic;
use think\App;
use think\Request;

class Article extends Base
{
    protected $logic = null;
    protected $category = null;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->logic    = new ArticleLogic();
        $this->category = new CategoryLogic();
    }

    /**
     * 文章列表
     * @return \think\response\View
     */
    public function index()
    {
        try {
            $list = $this->logic->getList();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        return view('article/index', compact('list'));
    }

    /**
     * 根据id获取单个文章信息
     * @param $id
     * @return array|mixed
     */
    protected function getOneArticleById($id)
    {
        try {
            $article_one = $this->logic->getDataOneById($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        if (!empty($article_one)) {
            return $article_one;
        }

        return [];
    }

    /**
     * 添加文章
     * @return \think\response\View
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = input("post.");
            try {
                $res = $this->logic->addArticle($data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($res) {
                $this->success("文章添加成功", url('Article/index'));
            }
            $this->error('文章添加失败');
        } else {
            $tree       = Tree::instance();
            $categories = $tree->init($this->category->getCategoryLists(), 'pid');
            $categories = $tree->getTreeList($tree->getTreeArray(0), 'title');

            return view('article/add', compact('categories'));
        }
    }

    /**
     * 查看文章详情
     * @return \think\response\View
     */
    public function read()
    {
        $id                  = input("param.id", 0, "intval");
        $article_one         = $this->getOneArticleById($id);
        $article_one['tags'] = explode(',', $article_one['tags']);

        return view('article/read', compact('article_one'));
    }

    /**
     * 编辑文章
     * @return \think\response\View
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $id         = input("post.id");
            $updateData = [
                'admin_id'    => session('uid'),
                'category_id' => input('post.category_id', 0, 'intval'),
                'title'       => input("post.title", "", "trim"),
                'tags'        => input("post.tags", "", "trim"),
                'body'        => input('post.body'),
                'is_top'      => input('post.is_top', 0, 'intval'),
                'is_comment'  => input('post.is_comment', 0, 'intval'),
                'status'      => input('post.status', 0, 'intval'),
            ];
            try {
                $res = $this->logic->updateCategory($id, $updateData);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            if ($res) {
                $this->success("文章编辑成功", url('Article/index'));
            }
            $this->error("文章编辑失败");
        } else {
            $id          = input("param.id", 0, 'intval');
            $article_one = $this->getOneArticleById($id);

            $tree       = Tree::instance();
            $categories = $tree->init($this->category->getCategoryLists(), 'pid');
            $categories = $tree->getTreeList($tree->getTreeArray(0), 'title');

            return view('article/edit', compact('article_one', 'categories'));
        }
    }

    /**
     * 删除文章
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(0, "请求类型不合法");
        }

        $id = $request->post('id', 0, 'intval');
        try {
            $result = $this->logic->deleteById($id);
        } catch (\Exception $e) {
            return writeJson(0, $e->getMessage());
        }

        if ($result) {
            return writeJson(1, "文章删除成功");
        }

        return writeJson(0, "文章删除失败");
    }

    /**
     * 回收站
     * @return \think\response\View
     */
    public function recycle()
    {
        try {
            $lists = \app\common\model\mysql\Article::onlyTrashed()->select();
        } catch (\Exception $e) {
            $lists = [];
        }


        return view('article/recycle', compact('lists'));
    }

    /**
     * 恢复被软删除的数据
     * $user = User::onlyTrashed()->find(1);
     * $user->restore();
     */
    public function restore()
    {
        $id = input('param.id', 0, 'intval');

        $result = $this->logic->restoreData($id);

        if ($result) {
            $this->success('数据恢复成功', url('Article/index'));
        }

        $this->error('数据恢复失败');
    }

    /**
     * 暂未使用此方法
     * @return \think\response\Json
     */
    public function destroyArt()
    {
        $id = input('post.id', 0, 'intval');

        $result = $this->logic->destroyArticle($id);


        if ($result) {
            return writeJson(1, "文章彻底删除成功");
        }

        return writeJson(0, "文章删除失败");
    }
}